﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Quality.spc
{
    public partial class Normaldistribution : Form
    {
        public Normaldistribution()
        {
            InitializeComponent();
            Double[] data = new Double[] {0.93462
,0.93897
,-0.08407
,-1.38217
,-0.29310
,-1.64274
,0.29240
,0.16139
,-0.38411
,-0.59806
,-1.42225
,-0.22474
,1.69669
,-1.08564
,0.02927
,-1.05369
,0.66923
,1.39834
,1.33680
,-0.57874
};

            int ybs = data.Length;
            int GroupNum = ProcessCababilityHelper.GetGroupNum(data.Length);
            int GroupSize = ProcessCababilityHelper.GetGroupSize(ybs, GroupNum);
            double ArrMax = SixSigmaHelper.GetArrMax(data);
            double ArrMin = SixSigmaHelper.GetArrMin(data);
            double AverageValue = SixSigmaHelper.GetAverageValue(data);

            double GroupDistance = ProcessCababilityHelper.GetGroupDistance(2, ArrMax, ArrMin, GroupNum, data);
            double[] GroupDownBound = ProcessCababilityHelper.GetGroupDownBound(ArrMin, GroupDistance, GroupNum);
            double[] GroupUpBound = ProcessCababilityHelper.GetGroupUpBound(ArrMin, GroupDistance, GroupNum);
            double[] GroupMedian = ProcessCababilityHelper.GetGroupMedian(ArrMin, GroupDistance, GroupNum);
            double[] DistributionDensity = ProcessCababilityHelper.GetDistributionDensity(GroupDistance, GroupDownBound, GroupUpBound, data);
            double[] GroupNormalDistribution = ProcessCababilityHelper.GetGroupNormalDistribution(GroupNum, data);
            double[] NormalDistribution = ProcessCababilityHelper.GetNormalDistribution( data);
            double GetTotalStdDev = SixSigmaHelper.GetTotalStdDev(data);
            bzc.Text = GetTotalStdDev.ToString();

           

            ChartArea chartArea = chart1.ChartAreas[0];
            chartArea.AxisX.Minimum = 0;
            chartArea.AxisX.Maximum = GroupNum+1;
            chartArea.AxisX.Interval = 1;
            

            chart1.Series.Clear();
            chart1.Series.Add("均值");//添加 
            chart1.Series[0].ChartType = SeriesChartType.Line;
            chart1.Series[0].Points.AddXY(AverageValue, 0);
            chart1.Series[0].Points.AddXY(AverageValue, SixSigmaHelper.GetArrMax(NormalDistribution));

            chart1.Series.Add("3sigma");//添加 
            chart1.Series[1].ChartType = SeriesChartType.Line;
            chart1.Series[1].Points.AddXY(AverageValue + 3 * GetTotalStdDev, 0);
            chart1.Series[1].Points.AddXY(AverageValue + 3 * GetTotalStdDev, SixSigmaHelper.GetArrMax(NormalDistribution));
            chart1.Series.Add("-3sigma");//添加 
            chart1.Series[2].ChartType = SeriesChartType.Line;
            chart1.Series[2].Points.AddXY(AverageValue - 3 * GetTotalStdDev, 0);
            chart1.Series[2].Points.AddXY(AverageValue - 3 * GetTotalStdDev, SixSigmaHelper.GetArrMax(NormalDistribution));

            chart1.Series.Add("正态");//添加 
            chart1.Series[3].ChartType = SeriesChartType.Spline;

            chart1.Series[3].YAxisType = AxisType.Secondary;
            for (int i = 0; i <GroupNum; i++)
            {

                chart1.Series[3].Points.AddXY(i+1 , NormalDistribution[i]);



            }

            //chart1.Series.Add(4.ToString());//添加 
            //chart1.Series[4].ChartType = SeriesChartType.Column;
            //for (int i = 0; i < GroupNum; i++)
            //{

            //    chart1.Series[4].Points.AddXY(i+1, DistributionDensity[i]);


            //}


            //List<double> datalist = new List<double>(data);
            //StandardDistribution mathX = new StandardDistribution(datalist);
            //Bitmap bitmap = mathX.GetGaussianDistributionGraph(800, 480, data.Length);
            //bitmap.Save(AppDomain.CurrentDomain.BaseDirectory + "/spc/tt.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);


            //pictureBox1.Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/spc/tt.jpg");


            //for (int i = 0; i <= GroupMedian.Length - 1; i++) { 
            
            // chart1.Series.Add(i.ToString());//添加 
            // chart1.Series[i.ToString()].ChartType = SeriesChartType.Column;
            // chart1.Series[i.ToString()].Points.AddXY(GroupMedian[i], DistributionDensity[i]);
            //}
               

           
            
          

        }
    }
}
